package com.zzyl.nursing.service.impl;

import java.util.List;
import java.util.Objects;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.service.INursingProjectService;
import com.zzyl.nursing.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 护理项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-31
 */
@Service
public class NursingProjectServiceImpl implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    private static final String KEY = "nursingProject:all";

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return nursingProjectMapper.selectNursingProjectById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        nursingProject.setCreateTime(DateUtils.getNowDate());
        int flag = nursingProjectMapper.insertNursingProject(nursingProject);
        deleteCache();
        return flag;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        nursingProject.setUpdateTime(DateUtils.getNowDate());
        int flag = nursingProjectMapper.updateNursingProject(nursingProject);
        deleteCache();
        return flag;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        int flag = nursingProjectMapper.deleteNursingProjectByIds(ids);
        deleteCache();
        return flag;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        int flag = nursingProjectMapper.deleteNursingProjectById(id);
        deleteCache();
        return flag;
    }

    /**
     * 查询所有护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAll() {
        List<NursingProjectVo> list = (List<NursingProjectVo>) redisTemplate.opsForValue().get(KEY);
        if (list != null && list.size() > 0){
            return list;
        }
        list = nursingProjectMapper.selectAll();
        redisTemplate.opsForValue().set(KEY,list);
        return list;
    }

    private void deleteCache() {
        redisTemplate.delete(KEY);
    }
}
